import os
import sys

def generate_markdown_from_py_files(directory, output_file):
    with open(output_file, 'w', encoding='utf-8') as md_file:
        for root, dirs, files in os.walk(directory):
            # 排除 venv 目录
            dirs[:] = [d for d in dirs if d != '.venv']
            dirs[:] = [d for d in dirs if d != 'env']
            dirs[:] = [d for d in dirs if d != 'scripts']
            dirs[:] = [d for d in dirs if d != 'build']
            dirs[:] = [d for d in dirs if d != '.vscode']
            dirs[:] = [d for d in dirs if d != 'node_modules']
            dirs[:] = [d for d in dirs if d != 'Scripts']
            dirs[:] = [d for d in dirs if d != 'resources']
            dirs[:] = [d for d in dirs if d != 'target']
            # 排除 src/js/lib
            if root.endswith(r'./src\js\lib'):
                continue
            for file in files:
                if file.endswith('.py') or \
                   file.endswith('.rs') or \
                   file.endswith('.js') or \
                   file.endswith('.html'):
                    file_path = os.path.join(root, file)
                    print(f'正在处理文件：{file_path}')
                    md_file.write(f'`{file_path}`\n')
                    # 获取文件后缀
                    file_suffix = os.path.splitext(file)[1].replace('.', '')
                    md_file.write(f'```{file_suffix}\n')
                    with open(file_path, 'r', encoding='utf-8') as py_file:
                        md_file.write(py_file.read())
                    md_file.write('\n```\n\n')

if __name__ == "__main__":
    # 指定目录和输出文件名
    target_directory = sys.argv[1]            # 替换为你的目标目录
    output_markdown_file = 'output.md'         # 输出的 Markdown 文件名

    generate_markdown_from_py_files(target_directory, output_markdown_file)
    print(f'Markdown 文件已生成：{output_markdown_file}')
